package com.example.musicplayer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.CheckBox;
import android.text.method.PasswordTransformationMethod;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.example.musicplayer.R;
import com.example.musicplayer.utils.Data;
import com.example.musicplayer.utils.GetAllActivity;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.ContentValues.TAG;





public class RegisterActivity extends AppCompatActivity {
    private Button btn_activity_register_register,btn_activity_register_exit;
    private CheckBox checkBox1,checkBox2,checkBox3;
    private EditText et_activity_register_username,et_activity_register_email,et_activity_register_password,et_activity_register_repassword,et_activity_register_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        btn_activity_register_register=(Button)findViewById(R.id.btn_activity_register_register);
        btn_activity_register_register.setOnClickListener(new RegisterActivity.btucl1());
        btn_activity_register_exit=(Button)findViewById(R.id.btn_activity_register_exit);
        btn_activity_register_exit.setOnClickListener(new RegisterActivity.btucl());
        checkBox1=(CheckBox) findViewById(R.id.check_box);
        checkBox2=(CheckBox) findViewById(R.id.check_box1);
        checkBox3=(CheckBox) findViewById(R.id.check_box2);

        et_activity_register_username=(EditText)findViewById(R.id.et_activity_register_username);
        et_activity_register_password=(EditText)findViewById(R.id.et_activity_register_password);
        et_activity_register_email=(EditText)findViewById(R.id.et_activity_register_email);
        et_activity_register_repassword=(EditText)findViewById(R.id.et_activity_register_repassword);
        et_activity_register_phone=(EditText)findViewById(R.id.et_activity_register_phone);

        GetAllActivity.getInstance().addActivity(this);
        et_activity_register_repassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        et_activity_register_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton mCompoundButton, boolean isChecked) {
                if(isChecked)
                {
                    et_activity_register_repassword.setTransformationMethod(null);
                    et_activity_register_password.setTransformationMethod(null);
                }
                else
                {
                    et_activity_register_repassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    et_activity_register_password.setTransformationMethod(PasswordTransformationMethod.getInstance());}
            }
        });
    }
    //退出事件
    class btucl implements View.OnClickListener {
        public void onClick(View v) {
           finish();
        }
    }
    //注册事件
    class btucl1 implements View.OnClickListener {
        public void onClick(View v) {
            String username = et_activity_register_username.getText().toString();
            String password = et_activity_register_password.getText().toString();
            String repassword = et_activity_register_repassword.getText().toString();
            String phone = et_activity_register_phone.getText().toString();
            String email = et_activity_register_email.getText().toString();

            if(password.equals(repassword)){
                //用户名重复校验
                String url = getString(R.string.web_ip)+"/register/check/"+username;
                OkHttpClient okHttpClient = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url(url)
                        .get()
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String data = response.body().string();
                        System.out.println(data);
                        if(data.equals("true")){
                            String url = getString(R.string.web_ip)+"/register";
                            OkHttpClient okHttpClient = new OkHttpClient();
                            MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                            JSONObject json = new JSONObject();
                            try {
                                json.put("username",username);
                                json.put("password", password);
                                json.put("phone", phone);
                                json.put("email", email);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            RequestBody body = RequestBody.create(JSON,json.toJSONString()); // new

                            final Request request = new Request.Builder()
                                    .url(url)
                                    .post(body)
                                    .build();
                            okHttpClient.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                    Looper.prepare();
                                    Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
                                    Looper.loop();
                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    String data = response.body().string();
                                    System.out.println(data);
                                    String url = getString(R.string.web_ip)+"/album";
                                    OkHttpClient okHttpClient = new OkHttpClient();
                                    MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                                    JSONObject json = new JSONObject();
                                    try {
                                        json.put("name","我喜欢的音乐");
                                        json.put("owner", data);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    RequestBody body = RequestBody.create(JSON,json.toJSONString()); // new
                                    final Request request = new Request.Builder()
                                            .url(url)
                                            .post(body)
                                            .build();
                                    okHttpClient.newCall(request).enqueue(new Callback() {
                                        @Override
                                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                            Log.d(TAG, "onResponse: 创建我喜欢歌单失败" );
                                        }

                                        @Override
                                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                            Log.d(TAG, "onResponse: 创建我喜欢歌单成功" );
                                        }
                                    });
                                    Looper.prepare();
                                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                                    Looper.loop();
                                }
                            });
                        }else{
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(), "当前用户名已存在", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                });
            }else{
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }).run();
            }
        }

    }
}

